package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdSensitiveDto;
import com.heima.model.wemedia.dtos.SensitivePageDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.model.wemedia.entity.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SensitiveServiceImpl implements SensitiveService {

    @Resource
    private SensitiveMapper sensitiveMapper;
    @Override
    public ResponseResult save(AdSensitiveDto dto) {

        WmSensitive sensitive = sensitiveMapper.selectOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(sensitive!=null){
            //当前数据库中有该敏感词
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在,请勿重复添加");

        }
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());


        sensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //列表查询
    @Override
    public ResponseResult list(SensitivePageDto dto) {
        Page<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotEmpty(dto.getName())){
            wrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        Page<WmSensitive> sensitivePage = sensitiveMapper.selectPage(page, wrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) sensitivePage.getTotal());
        result.setData(sensitivePage.getRecords());

        return result;

    }

    /**
     * 更新敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult update(AdSensitiveDto dto) {
        //参数校验
        if(dto.getId()==null||StringUtils.isNotEmpty(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        WmSensitive wmSensitive = sensitiveMapper.selectById(dto.getId());
        if(wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改敏感词
        wmSensitive.setSensitives(dto.getSensitives());
        //更新数据
        sensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据id查询
        WmSensitive wmSensitive = sensitiveMapper.selectById(id);
        if(wmSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        sensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
